package com.itheima;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.function.Consumer;


public class ArrayListDemo {
    public static void main(String[] args) {
        // 创建ArrayList对象
        ArrayList<String> list = new ArrayList<>();
        // 添加元素
        list.add("ahangsan");
        list.add(1, "lisi");
        list.set(0, "wangwu");
        list.remove(1);
        System.out.println(list);
        System.out.println("------------1-----------");
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            System.out.println(s);
        }

        System.out.println("------------2----------");
        for (String obj : list) {
            System.out.println(obj);
        }

        System.out.println("------------3----------");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }

        System.out.println("------------4 lambda表达式（函数 表达式）----------");
        // lambda表达式
        list.forEach(System.out::println);

        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        // Iterates over each element in the list and prints it
        list.forEach(System.out::println);


    }
}
